package com.example.service.impl;

import cn.dev33.satoken.model.wrapperInfo.SaDisableWrapperInfo;
import cn.dev33.satoken.stp.StpInterface;
import com.example.constant.RoleTypeEnum;
import com.example.mapper.UserMapper;
import com.example.model.pojo.Person;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zhexueqi
 * @ClassName StpInterfaceImpl
 * @since 2025/4/8 13:57
 */
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        return List.of();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        int roleId = userMapper.selectRole((int) loginId);
        String role = Objects.requireNonNull(RoleTypeEnum.getByCode(roleId)).getType();
        return List.of(role);
    }

    @Override
    public SaDisableWrapperInfo isDisabled(Object loginId, String service) {
        Person person = userMapper.selectById((Serializable) loginId);
        if (person.getStatus() == 1) {
            return SaDisableWrapperInfo.createNotDisabled();
        }

        // 根据封禁类型和当前服务类型判断是否应该禁止
        Integer banType = person.getBanType();
        if (banType != null) {
            // 1-资金操作 2-敏感操作 3-全部封禁
            if ((banType == 1 && "financial_operations".equals(service)) ||
                    (banType == 2 && "sensitive_operations".equals(service)) ||
                    banType == 3) {
                LocalDateTime banExpiryTime = person.getBanExpiryTime();
                return new SaDisableWrapperInfo(true, banExpiryTime.getSecond(), 1);
            }
        }

        return SaDisableWrapperInfo.createNotDisabled();
    }
}
